package cn.edu.buaa.sei.jdat.vt;

import cn.edu.buaa.sei.jdat.model.Jar;
import cn.edu.buaa.sei.jdat.model.Member;
import cn.edu.buaa.sei.jdat.model.Package;
import cn.edu.buaa.sei.jdat.model.Type;
import cn.edu.buaa.sei.jdat.vt.widgets.Bubble;
import cn.edu.buaa.sei.jdat.vt.widgets.JavaElementListView;


public class NavigateView extends MainView.Tab {
	
	// controls
	private final Bubble titleBubble;
	private final Bubble typeBubble;
	private final JavaElementListView packageList;
	private final JavaElementListView typeList;
	private final JavaElementListView memberList;
	
	// layout
	private int margin = 4;
	private int listViewWidth = 250;
	private int packageListViewHeight = 250;
	
	// model
	private Jar selectedJar;

	public NavigateView(MainView owner) {
		super(owner);
		selectedJar = null;
		addSubview(titleBubble = new Bubble.BrownBubble(0, 0, 0, 0) {
			{
				setHeight(getPreferredSize().y);
			}
		});
		addSubview(packageList = new JavaElementListView(0, 0, 0, 0) {
			@Override
			protected void selectedItemChanged(Item item) {
				// show types
				if (item != null) {
					typeList.removeAllElements();
					for (Type t : ((Package) item.getElement()).getTypes()) {
						typeList.addElement(t);
					}
				}
			}
		});
		addSubview(typeList = new JavaElementListView(0, 0, 0, 0) {
			@Override
			protected void selectedItemChanged(Item item) {
				// show type members
				if (item != null) {
					typeBubble.setText(item.getElement().getSignature());
					memberList.removeAllElements();
					for (Member member : ((Type) item.getElement()).getMembers()) {
						memberList.addElement(member);
					}
				} else {
					typeBubble.setText("No Type Selected");
					memberList.removeAllElements();
				}
			}
		});
		addSubview(typeBubble = new Bubble.BrownBubble(0, 0, 0, 0) {
			{
				setHeight(getPreferredSize().y);
			}
		});
		addSubview(memberList = new JavaElementListView(0, 0, 0, 0) {
		});
	}

	public void showJar(Jar jar) {
		clear();
		selectedJar = jar;
		titleBubble.setText(jar.getName());
		
		// show package
		for (Package p : jar.packages) {
			packageList.addElement(p);
		}
	}
	
	public void clear() {
		titleBubble.setText("No Jar Selected");
		packageList.removeAllElements();
		typeList.removeAllElements();
		typeBubble.setText("No Type Selected");
		memberList.removeAllElements();
	}

	@Override
	protected void activated() {
		if (selectedJar == null) {
			clear();
		}
	}

	@Override
	protected void deactivated() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void layout() {
		titleBubble.setPosition(margin - 1, margin);
		titleBubble.setWidth(listViewWidth + 2);
		packageList.setPosition(margin, titleBubble.getTop() + titleBubble.getHeight() + margin);
		packageList.setSize(listViewWidth, packageListViewHeight);
		typeList.setPosition(margin, packageList.getTop() + packageListViewHeight + margin);
		typeList.setWidth(listViewWidth);
		typeList.setBottom(margin);
		
		typeBubble.setPosition(margin + listViewWidth + margin - 2, margin);
		typeBubble.setRight(margin - 1);
		memberList.setPosition(listViewWidth + margin * 2, typeBubble.getTop() + typeBubble.getHeight() + margin);
		memberList.setRight(margin);
		memberList.setBottom(margin);
	}

}
